package com.jvtd.eyl.widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.util.AttributeSet;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.bean.ui.HomeBean;
import com.jvtd.eyl.constants.App;
import com.jvtd.eyl.ui.main.home.allPeriodical.AllPeriodicalActivity;
import com.jvtd.eyl.ui.main.home.classInfo.ClassInfoActivity;
import com.jvtd.eyl.ui.main.home.periodicalInfo.PeriodicalInfoActivity;
import com.jvtd.eyl.widget.adapter.HomeAdapter;
import com.jvtd.widget.emptyView.JvtdEmptyView;
import com.jvtd.widget.recyclerView.JvtdRecyclerView;

/**
 * 精选列表
 *
 * 作者:chenlei
 * 时间:2018/11/12 4:33 PM
 */
public class HomeRecyclerView extends JvtdRecyclerView<HomeBean> {
    private HomeAdapter mAdapter;

    public HomeRecyclerView(@NonNull Context context) {
        super(context);
    }

    public HomeRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        mAdapter = new HomeAdapter();
//        mAdapter.openLoadAnimation();
        mAdapter.setSpanSizeLookup((gridLayoutManager, position) -> {
            int type = getData().get(position).getType();
            if (type == HomeBean.TYPE_CLASS || type == HomeBean.TYPE_PERIODICAL) {
                return 1;
            } else if (type == HomeBean.TYPE_TITLE){
                return 4;
            }
            return 0;
        });
        mAdapter.setHeaderViewAsFlow(true);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            HomeBean bean = mAdapter.getItem(position);
            if (bean != null) {
                if (bean.getType() == HomeBean.TYPE_CLASS){
                    //跳转分类
                    mContext.startActivity(ClassInfoActivity.getIntent(mContext,bean.getClassBean()));
                }else if(bean.getType() == HomeBean.TYPE_PERIODICAL) {
                    //跳转期刊详情
                    mContext.startActivity(PeriodicalInfoActivity.getIntent(mContext, bean.getPeriodicalBean()));
                }
            }
        });
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.more_btn){
                //跳转全部期刊
                mContext.startActivity(AllPeriodicalActivity.getIntent(mContext));
            }
        });
        return mAdapter;
    }

    @Override
    public LayoutManager initLayoutManager() {
        return new GridLayoutManager(mContext, 4);
    }

    @Override
    public boolean isFirst() {
        return false;
    }

    @Override
    public int getLoadNum() {
        return 0;
    }

    @Override
    protected void configEmptyView(JvtdEmptyView emptyView) {
        emptyView.setEmptyImage(0)
                .setErrorText(R.string.loading_failed_touch)
                .setErrorImage(0)
                .setLoadingText("")
                .setEmptyText(R.string.empty_book_tips);
    }

    @Override
    public double getFlingRatio()
    {
        return App.FLING_RATIO;
    }
}
